<?php

class Application_Model_UserHasEmail extends Ahs_Model
{
    /**
     * @var integer
     */
    protected $_id;
    /**
     * @var integer
     */
    protected $_user_id;
    /**
     * @var integer
     */
    protected $_type_id;
    /**
     * @var string
     */
    protected $_email;
    /**
     * @var boolean
     */
    protected $_isprimary;
    
    
    /**
     * @return integer
     */    
    public function getId() {
        return $this->_id;
    }
    /**
     * @param integer $user_id
     */
    public function setId($id) {
        $this->_id = $id;
    }
    
    /**
     * @return integer
     */    
    public function getUserId() {
        return $this->_user_id;
    }
    /**
     * @param integer $user_id
     */
    public function setUserId($user_id) {
        $this->_user_id = $user_id;
    }
    /**
     * Get the user object
     * @return Application_Model_User
     */
    public function getUser()
    {
        if($this->_user_id != null)
        {
            $mapper = new Application_Model_UserMapper();
            return $mapper->fetchById($this->_user_id);
        }
        
        return null;
    }

    /**
     * @return integer
     */
    public function getTypeId() {
        return $this->_type_id;
    }
    /**
     * @param integer $type_id
     */
    public function setTypeId($type_id) {
        $this->_type_id = $type_id;
    }
    /**
     * Get the EmailType object
     * @return Application_Model_EmailType
     */
    public function getType()
    {
        if($this->_type_id != null)
        {
            $mapper = new Application_Model_EmailTypeMapper();
            return $mapper->fetchById($this->_type_id);
        }
        
        return null;
    }
    
    /**
     * @return string
     */
    public function getEmail() {
        return $this->_email;
    }
    /**
     * @param string $email
     */
    public function setEmail($email) {
        $this->_email = $email;
    }
    
    /**
     * @return boolean
     */    
    public function getIsPrimary() {
        return $this->_isprimary;
    }
    /**
     * @param boolean $isprimary
     */
    public function setIsPrimary($isprimary) {
        $this->_isprimary = ($isprimary ? 1 : 0);
    }
    
    /**
     * Parses the object to a string that can be used in the custom UserEmail form element
     * 
     * @return string 
     */
    public function toFormValue()
    {
        if($this->_email !== null && $this->_type_id !== null)
            return $this->_id."*".$this->_email."*".$this->_type_id;
        
        return "";
    }
    
    /**
     * Creates an instance of this model
     * @param type $id
     * @param type $user_id
     * @param type $email
     * @param type $type_id
     * @param type $isprimary 
     */
    public function __construct($id = null, $user_id = null, $email = null, $type_id = null, $isprimary = 0)
    {
        $this->setId($id ? $id : null);
        $this->setUserid($user_id);
        $this->setEmail($email);
        $this->setTypeId($type_id);
        $this->setIsPrimary($isprimary);
    }
}

